import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import fortune from './modules/fortune'
import hehun from './modules/hehun'
import payment from './modules/payment'
import system from './modules/system'

Vue.use(Vuex)

// Vuex持久化插件
const vuexPersist = (store) => {
	// 从localStorage恢复状态
	const savedState = localStorage.getItem('vuex-state')
	if (savedState) {
		try {
			const parsedState = JSON.parse(savedState)
			store.replaceState(parsedState)
		} catch (e) {
			console.error('Failed to restore Vuex state:', e)
		}
	}

	// 监听mutation，保存状态到localStorage
	store.subscribe((mutation, state) => {
		localStorage.setItem('vuex-state', JSON.stringify(state))
	})
}

const store = new Vuex.Store({
	modules: {
		system,
		fortune,
		hehun,
		payment
	},
	getters,
	plugins: [vuexPersist]
})

export default store